import 'package:cunyutech_flutter_ui/cunyutech_flutter_ui.dart';
import 'package:cunyutech_flutter_ui/src/layout/HeaderContainer.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_dva/flutter_dva.dart';

class ProxySettingsPage extends Component {
  ProxySettingsPage({Key key}) : super(key: key);

  @override
  _ProxySettingsPageState createState() => _ProxySettingsPageState();
}

class _ProxySettingsPageState extends Connect<CYApplicationStateTmpl, ProxySettingsPage> {
  _ProxySettingsPageState() : super(Dva.stateCreator<CYApplicationStateTmpl>("cyapplication"), Dva.actionCreator({
    "saveProxySettings": "cyapplication/saveProxySettings",
  }));

  /// http代理地址
  String httpProxy;
  /// https代理地址
  String httpsProxy;
  /// 是否开启代理
  bool enableProxy = false;

  @override
  void initState() {
    httpProxy = state.httpProxy;
    httpsProxy = state.httpsProxy;
    enableProxy = state.enableProxy ?? false;
    super.initState();
  }

  saveProxySettings() async {
    await props["saveProxySettings"]({
      "httpProxy": httpProxy,
      "httpsProxy": httpsProxy,
      "enableProxy": enableProxy,
    });
  }
  
  @override
  Widget build(BuildContext context) {
    var cs = CYThemeData.of(context);
    
    return Scaffold(
      appBar: HeaderContainer(
        child: CYTitle("代理设置"),
      ),
      backgroundColor: cs.primaryBackColor,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          SizedBox(height: CYHeight.Large,),
          GestureDetector(
            onTap: () {
              enableProxy = !enableProxy;
              setState(() {});
            },
            child: InputContainer(
              margin: EdgeInsets.symmetric(horizontal: CYMargin.Large),
              backgroundColor: enableProxy ? cs.primaryColor : Colors.transparent,
              child: CYParagraph(enableProxy ? "关闭代理" : "开启代理", alignment: Alignment.center, lineHeight: null,),
            )
          ),
          Column(
            children: enableProxy ? [
              SizedBox(height: CYMargin.Default,),
              InputContainer(
                margin: EdgeInsets.symmetric(horizontal: CYMargin.Large),
                child: CYTextInput(
                  height: 38,
                  alignment: Alignment.centerLeft,
                  maxLines: 1,
                  decoration: BoxDecoration(),
                  placeholder: "http://127.0.0.0:8888",
                  placeholderStyle: TextStyle(
                    fontSize: CYFontSize.Middle, 
                    fontWeight: FontWeight.w600, 
                    color: cs.foreColorSystem.placeholderColor,
                  ),
                  value: httpProxy,
                  maxLength: 30,
                  onChange: (String value) {
                    httpProxy = value;
                    setState(() {});
                  },
                  keyboardType: TextInputType.url,
                  padding: EdgeInsets.only(left: CYMargin.XSmall),
                )
              ),
              SizedBox(height: CYMargin.Default,),
              InputContainer(
                margin: EdgeInsets.symmetric(horizontal: CYMargin.Large),
                child: CYTextInput(
                  height: 38,
                  alignment: Alignment.centerLeft,
                  maxLines: 1,
                  decoration: BoxDecoration(),
                  placeholder: "https://127.0.0.0:8888",
                  placeholderStyle: TextStyle(
                    fontSize: CYFontSize.Middle, 
                    fontWeight: FontWeight.w600, 
                    color: cs.foreColorSystem.placeholderColor,
                  ),
                  value: httpsProxy,
                  maxLength: 30,
                  onChange: (String value) {
                    httpsProxy = value;
                    setState(() {});
                  },
                  keyboardType: TextInputType.url,
                  padding: EdgeInsets.only(left: CYMargin.XSmall),
                )
              ),
            ] : []
          ),
          SizedBox(height: CYHeight.Large,),
          PrimaryButton(
            title: "保存",
            onTap: saveProxySettings,
            borderRadius: CYBorderRadius.Default,
            margin: EdgeInsets.symmetric(horizontal: CYMargin.Large),
          )
        ]
      ),
    );
  }
}